<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Handlers\WeconHandler;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Arr;

class DedustingDevicePoint extends Model
{
    const POINTS = [
        'pressure' => '压力',
        'temperature' => '温度',
        'flow' => '流量',
        'concentration' => '浓度',
        'input_dew_point' => '进气露点',
        'ozone_output' => '臭氧产量',
        'oxygen_leakage' => '氧气泄露',
        'ozone_leakage' => '臭氧泄露',
    ];

    const POINT_UNITS = [
        'pressure' => 'Pa',
        'temperature' => '℃',
        'flow' => 'm³/h',
        'concentration' => 'g/m³',
        'input_dew_point' => 'Pa',
        'ozone_output' => 'mg/h',
        'oxygen_leakage' => 'mg/h',
        'ozone_leakage' => 'mg/h',
    ];

    const POINT_RANGE = [
        'pressure' => '10Pa ~ 20Pa',
        'temperature' => '10℃ ~ 20℃',
        'flow' => '10m³/h ~ 20m³/h',
        'concentration' => '10g/m ~ 20g/m',
        'input_dew_point' => '10Pa ~ 20Pa',
        'ozone_output' => '10mg/h ~ 20mg/h',
        'oxygen_leakage' => '10mg/h ~ 20mg/h',
        'ozone_leakage' => '10mg/h ~ 20mg/h',
    ];

    const POINT_MODE = [
        '0' => '手动',
        '1' => '自动'
    ];

    protected $table = 'xfy_dedusting_device_points';

    public function repairs()
    {
        return $this->hasMany(DedustingRepair::class);
    }

    public static function ranges($deviceId)
    {
        $ranges = [];

        $cache = DedustingPointRange::getCache($deviceId);
        if ($cache) {
            $ranges = Arr::pluck($cache, null, 'point');            
        } else {
            foreach (self::POINT_UNITS as $point => $unit) {
                $ranges[$point]['point'] = $point;
                $ranges[$point]['point_text'] = self::POINTS[$point];
                $ranges[$point]['unit'] = $unit;
                $ranges[$point]['min'] = 0;
                $ranges[$point]['max'] = 0;

            }
        }
        return $ranges;
    }
}